package com.bfly.mall.system.service.impl;

import com.bfly.core.base.service.BaseServiceImpl;
import com.bfly.core.enums.SysError;
import com.bfly.core.exception.ServiceResponseException;
import com.bfly.mall.system.dao.DictionaryMapper;
import com.bfly.mall.system.entity.Dictionary;
import com.bfly.mall.system.dto.DictionaryFormDTO;
import com.bfly.mall.system.service.IDictionaryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 长沙商脉科技有限公司
 * @date 2018/12/14 11:11
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
public class DictionaryServiceImpl extends BaseServiceImpl<Dictionary, Integer> implements IDictionaryService {

    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(DictionaryFormDTO formDTO) {
        Dictionary dictionary = new Dictionary();
        BeanUtils.copyProperties(formDTO, dictionary);
        dictionary.setCreateDate(new Date());
        dictionary.setUpdateDate(dictionary.getCreateDate());
        super.save(dictionary);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(DictionaryFormDTO formDTO) {
        Dictionary dictionary = get(formDTO.getId());
        if (dictionary == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "不存在该字典信息!");
        }
        BeanUtils.copyProperties(formDTO, dictionary);
        dictionary.setUpdateDate(new Date());
        super.edit(dictionary);
    }

    @Override
    public List<Dictionary> getList(Map<String, Object> exactQueryProperty) {
        Map<String, Boolean> sortMap = new HashMap<>(1);
        sortMap.put("seq", true);
        return super.getList(exactQueryProperty, null, sortMap);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(Dictionary dictionary) {
        boolean exist = checkExist(dictionary);
        Assert.isTrue(!exist, "该数据字典数据已重复!");
        return super.save(dictionary);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean edit(Dictionary dictionary) {
        boolean exist = checkExist(dictionary);
        Assert.isTrue(!exist, "该数据字典数据已重复!");
        return super.edit(dictionary);
    }

    /**
     * 检查是否存在相同的数据字典
     *
     * @author 长沙商脉科技有限公司
     * @date 2018/12/14 11:34
     */
    private boolean checkExist(Dictionary dictionary) {
        Dictionary dic = get(new HashMap<String, Object>(3) {
            private static final long serialVersionUID = 6340025449740664103L;

            {
                put("name", dictionary.getName().trim());
                put("value", dictionary.getValue());
                put("type", dictionary.getType().trim());
            }
        });
        return dic != null && (dic.getId() != dictionary.getId());
    }

    @Override
    public List<String> getTypes() {
        return dictionaryMapper.getTypes();
    }
}
